﻿using System;
using System.Collections.Generic;
using System.Linq;
using Bricks.Injecting;
using Spring.Objects.Factory;

namespace Bricks.Spring.Injecting
{

    /// <summary>
    /// 
    /// </summary>
    public class ObjectFactoryFacade : ContainerFacadeBase<IListableObjectFactory>, IObjectFactoryFacade
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectFactory"></param>
        public ObjectFactoryFacade(IListableObjectFactory objectFactory) :
            base(objectFactory)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public override object Resolve(Type serviceType, string key)
        {
            return Execute<object>(objectFactory => objectFactory.GetObject(key, serviceType));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public override IEnumerable<object> ResolveAll(Type serviceType)
        {
            return Execute<IEnumerable<object>>(objectFactory => objectFactory.GetObjectsOfType(serviceType).Values.Cast<object>());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public override TService Resolve<TService>(string key)
        {
            return (TService)Resolve(typeof(TService), key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public override IEnumerable<TService> ResolveAll<TService>()
        {
            return Execute<IEnumerable<TService>>(objectFactory => objectFactory.GetObjectsOfType(typeof(TService)).Values.Cast<TService>());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IContainerFacade CreateChild()
        {
            throw new NotImplementedException();
        }
    }
}
